<template>
  <div class="activity-type" v-loading.fullscreen.lock="loading">
    <page-main :pagination="pagination" @search-data="searchData" @page-change="pageChange">
      <template slot="search">
        <search-form :search="search"></search-form>
      </template>
      <template slot="append">
        <el-button type="primary" size="medium" @click="addActivity">新增</el-button>
      </template>
      <template slot="table">
        <el-table fit stripe border highlight-current-row :data="activityList" height="100%">
          <el-table-column prop="activityTypeName" label="类型名称" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="状态" width="80px" align="center">
            <template slot-scope="scope">
              <span :class="'status_'+scope.row.status">{{i18n.getMsg(`goods.activityType.status.${scope.row.status}`)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="updateActivity(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </page-main>

    <el-dialog :title="dialogTitle[dialogType]" custom-class="sys-dialog" :close-on-click-modal="false" :visible.sync="openDialog" width="450px"
      top="calc(50vh - 105px)" @closed="dialogClosed">
      <div style="height:150px;oveflow:auto">
        <activity-form v-if="openDialog" :activity="selectActivity" :type="dialogType" @save-success="saveSuccess"></activity-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import GoodsApi from 'services/goodsApi';

  import PageMain from 'commonComponents/pageMain';
  import SearchForm from './searchForm';
  import ActivityForm from './activityForm';

  import Pagination from 'models/pagination';
  import ActivityTypeSearch from 'models/goods/activityTypeSearch';

  export default {
    data() {
      return {
        loading: false,
        search: new ActivityTypeSearch(),
        pagination: new Pagination(),
        activityList: [],
        openDialog: false,
        dialogType: '',
        selectActivity: null,
        dialogTitle: {
          add: "新增活动类型",
          update: "修改活动类型"
        }
      };
    },

    created() {
      this.setActivityList(true);
    },

    methods: {
      setActivityList(isSearch = false) {
        let search = this.search;
        if (!isSearch) {
          search.resetOldData();
        }

        let pagination = this.pagination;
        this.loading = true;
        GoodsApi.getActivityList(search, pagination).then(activityList => {
          if (isSearch) {
            search.setOldData();
          }

          if (activityList.length === 0 && pagination.currentPage > 1) {
            this.$nextTick(() => {
              this.setActivityList(isSearch);
            });
            return;
          }
          this.activityList = activityList;
          this.loading = false;
        }).catch((error) => {
          this.loading = false;
        });
      },

      searchData() {
        this.pagination.currentPage = 1;
        this.setActivityList(true);
      },

      pageChange() {
        this.setActivityList();
      },

      addActivity() {
        this.dialogType = 'add';
        this.openDialog = true;
      },

      updateActivity(activity) {
        this.dialogType = 'update';
        this.selectActivity = activity;
        this.openDialog = true;
      },

      saveSuccess() {
        this.openDialog = false;
        this.setActivityList();
      },

      dialogClosed() {
        this.dialogType = '';
        this.selectActivity = null;
      }
    },

    components: {
      PageMain,
      SearchForm,
      ActivityForm
    }
  };

</script>

<style lang="scss" scoped>
  .activity-type {
    height: 100%;

    .status_0 {
      color: #ff0000;
    }

    .status_1 {
      color: #11b95c;
    }
  }

</style>
